<template>
  <a-row :gutter="24" >
    <a-col :md="4" :sm="24">
      <a-card :bordered="false" :loading="treeLoading">
        <a-directory-tree
          :showIcon="false"
          :treeData="treeList"
          v-if="treeList.length"
          @select="handleClick"
          :defaultExpandAll="true"
          :replaceFields="replaceFields" />
      </a-card>
    </a-col>

    <a-col :md="19" :sm="24">
      <a-card :bordered="false">
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="48">

              <a-col :md="8" :sm="24">
                <a-form-item label="网关">
                  <a-select style="width: 100%" v-model="queryParam.gatewayCode" allow-clear placeholder="请选择网关" >
                    <a-select-option v-for="(item,ind) in gatewayList" :key="ind" :value="item.gatewayCode" >{{ item.gatewayCode }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>

              <a-col :md="!advanced && 8 || 24" :sm="24">
                <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                  <a-button type="primary" v-if="hasPerm('bizGatewayStartupLog:add')" @click="$refs.addForm.add()" class="btn_add" icon="plus">新增</a-button>
                  <a-button type="primary" @click="$refs.table.refresh(true)" class="btn_search_reset">查询</a-button>
                  <a-button style="margin-left: 8px" @click="() => queryParam = {}" class="btn_search_reset">重置</a-button>
                </span>
              </a-col>

            </a-row>
          </a-form>
        </div>

        <s-table
          ref="table"
          size="default"
          :columns="columns"
          :data="loadData"
          :alert="true"
          :rowKey="(record) => record.id"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        >

          <span slot="type" slot-scope="text">
            {{ getType(text) }}
          </span>

          <span slot="action" slot-scope="text, record">
            <a @click="$refs.editForm.edit(record)" class="btn_edit" v-if="hasPerm('bizGatewayStartupLog:edit')">编辑</a>
            <a-divider type="vertical" v-if="hasPerm('bizGatewayStartupLog:edit')"/>
            <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">
              <a class="btn_more">删除</a>
            </a-popconfirm>
          </span>
        </s-table>

        <add-form ref="addForm" @ok="handleOk"/>
        <edit-form ref="editForm" @ok="handleOk"/>

      </a-card>
    </a-col>
  </a-row></template>

<script>
  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/bizgatewaystartuplog/bizGatewayStartupLogManage'
  import addForm from './addForm'
  import editForm from './editForm'
  import { treeList } from '@/api/modular/main/bizstation/bizStationManage'
  import { gatewayList } from '@/api/modular/main/bizgateway/bizGatewayManage'

  export default {
    components: {
      STable,
      addForm,
      editForm
    },

    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '网关编号',
            dataIndex: 'gatewayCode'
          },
          {
            title: '网关ip',
            dataIndex: 'ip'
          },
          {
            title: '类型',
            dataIndex: 'type',
            scopedSlots: { customRender: 'type' }
          },
          {
            title: '时间',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        treeList: [],
        treeLoading: true,
        gatewayList: [],
        stationId: ''
      }
    },
    created () {
      this.getStationTree()
    },
    methods: {
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      getType (text) {
        if (Number(text) === 0) {
          return '启动'
        }
      },
      getStationTree () {
        treeList(Object.assign(this.queryParam)).then(res => {
          this.treeLoading = false
          if (res.success) {
            this.treeList = res.data
          }
        })
      },
      getGatewayList (stationId) {
        gatewayList({ stationId: stationId }).then(res => {
          if (res.success) {
            this.gatewayList = res.data
            this.gatewayCodes = []
            this.gatewayList.filter(value => {
              this.gatewayCodes.push(value.gatewayCode)
            })
            this.queryParam.gatewayCodes = this.gatewayCodes.toString()
            this.handleOk()
          }
        })
      },
      handleClick (e) {
        this.queryParam = {}
        this.stationId = e.toString()
        this.getGatewayList(e.toString())
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
